/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
  ListNode* rotateRight(ListNode* head, int k) {
    if (!head) return head;
    auto dummy = new ListNode{0, head};
    int len = 0;
    auto T = dummy;
    for (; T->next; T = T->next) len ++;
    if (k % len == 0) return dummy->next;
    int step = len - k % len;
    auto K = dummy;
    while (step --) K = K->next;
    auto F = dummy->next, N = K->next;
    T->next = F;
    dummy->next = N;
    K->next = nullptr;
    return dummy->next;
  }
};